﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace server.Tool
{
    /// <summary>
    /// 扩展工具
    /// </summary>
    public static class MySqlExtendTools
    {
        /// <summary>
        /// 通过主键查找实体对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static T GetEntityObjectByPrimaryKey<T>(string id, string tableName) where T : new()
        {
            string sql = "select * from " + tableName + " where id = @id";
            List<T> tList = MySqlExecuteTools.GetObjectResult<T>(sql, new MySqlParameter[] { new MySqlParameter("@id", id) });

            if (tList != null && tList.Count > 0)
            {
                return tList[0];
            }

            return default(T);
        }

        public static void DeleteByPrimaryKey(int id, string tableName)
        {
            string sql = "delete from " + tableName + " where id = " + id;
            MySqlExecuteTools.AddOrUpdate(sql);

        }

        public static List<T> GetAllObject<T>( string tableName) where T : new()
        {
            string sql = "select * from " + tableName ;
            List<T> tList = MySqlExecuteTools.GetObjectResult<T>(sql, null);
            return tList;
        }
    }

}
